package com.zxh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.zxh.entity.Applicant;
import com.zxh.utils.DBUtils;

public class ApplicantDao {
	/**
	 * 求职者登录
	 * 
	 * @param email
	 * @param password
	 * @return
	 */
	public Applicant login(String email,String password){
		Connection con =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		Applicant applicant = new Applicant();
		String sql = "select * from Applicant where applicant_email=? and applicant_pwd=?";// 登录的sql语句		
		try {
			 con= DBUtils.getConnection();//获取数据库连接
			 ps = con.prepareStatement(sql);//执行预编译的sql语句
			 ps.setString(1, email);//设置参数
			 ps.setString(2, password);
			 rs = ps.executeQuery();//执行查询
			while(rs.next()){
					applicant.setApplicant_email(rs.getString("applicant_email"));
					applicant.setApplicant_pwd(rs.getString("applicant_pwd"));
					applicant.setApplicant_id(rs.getInt("Applicant_id"));
					applicant.setUsertype(rs.getString("usertype"));
				}
			 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}//获取数据库连接
		
		return  applicant;
	}
	/**
	 * 判断邮箱是否已经存在
	 * @param email
	 * @return
	 */
	public  boolean IsExitemail(String email){
		Connection con =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		boolean temp =false;
		String sql="select * from Applicant where Applicant_email=?";//登录验证
		
		try {
			 con= DBUtils.getConnection();//获取数据库连接
			 ps = con.prepareStatement(sql);//执行预编译的sql语句
			 ps.setString(1, email);//设置参数
			 rs = ps.executeQuery();//执行查询
			 while(rs.next()){
				 temp = true;
			 }
			 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}//获取数据库连接
		
		return  temp;
	}
	
	
	/**
	 * 保存求职者信息
	 */
	public void save(String email,String password,String usertype){
		Connection con =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		String sql ="insert into Applicant(Applicant_email,Applicant_pwd,Applicant_registdate,usertype) values(?,?,?,?)";
				try {
					 con= DBUtils.getConnection();//获取数据库连接
					 ps = con.prepareStatement(sql);//执行预编译的sql语句
					 ps.setString(1, email);//设置参数
					 ps.setString(2,password);
					 ps.setTimestamp(3,new Timestamp(new Date().getTime()));
					 ps.setString(4, usertype);//设置参数
					 ps.executeUpdate();//执行更新操作
					
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					DBUtils.close(con, rs, null, ps);
				}//获取数据库连接
	}
	
	
}
